﻿Public Class CouncilManagement
    Implements SuperForm

    Private councilDA As DataTable
    Private detailDA As DataTable

    Private _curRowCouncilSelected As Integer = -1
    Private _curRowDetailSelected As Integer = -1

    Private Sub CouncilManagement_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        loadAll()
    End Sub

    Public Function loadAll() As Boolean Implements SuperForm.loadAll
        loadCouncil()
        loadDetail()
        specifyCorrespondingDetail()
    End Function

    Public Function specifyCorrespondingDetail() As Boolean

        If (Me.detailDA Is Nothing Or Me.councilDA Is Nothing) Then
            Return False
        End If

        If (Me._curRowCouncilSelected < 0 Or Me._curRowCouncilSelected > (Me.councilDA.Rows.Count - 1)) Then
            Return False
        End If

        Dim councilID As Integer = Me.councilDA.Rows(Me._curRowCouncilSelected)(I_Council.ColumnName_id)

        '' Xác định Detail tương ứng
        Dim da As DataTable = New DataTable
        da = Me.detailDA.Clone
        Dim i As Integer = 0
        For i = 0 To Me.detailDA.Rows.Count - 1
            If (Me.detailDA.Rows(i)(I_Person.ColumnName_CouncilID).Equals(councilID)) Then
                Dim daRow As DataRow = Me.detailDA.Rows(i)
                da.ImportRow(daRow)
                Me._curRowDetailSelected = i
                Exit For
            End If
        Next

        Me.grvPerson.DataSource = da
    End Function

    Public Function loadCouncil() As Boolean Implements SuperForm.loadCouncil
        Dim x_council As New X_Council(-1)
        Me.councilDA = New Query(x_council, Nothing).dataTable
        Me.grvCouncil.DataSource = Me.councilDA

        Me.grvCouncil.AutoGenerateColumns = True

        Dim i As Integer = 0
        For i = 0 To Me.grvCouncil.ColumnCount - 1
            Me.grvCouncil.Columns(i).HeaderText = x_council.getPoInfo.getPOInfoColumn(i).getColumnName.ToUpper
        Next
        If (Not Me.councilDA Is Nothing) Then
            Me._curRowCouncilSelected = 0
            Me.grvCouncil.SelectionMode = DataGridViewSelectionMode.RowHeaderSelect
            Me.grvCouncil.Rows(0).Selected = True
        End If

    End Function

    Public Function loadDetail() As Boolean Implements SuperForm.loadDetail
        Dim x_person As New X_Person(-1)
        Me.detailDA = New Query(x_person, Nothing).dataTable
        Me.grvPerson.DataSource = Me.detailDA

        Dim i As Integer = 0
        For i = 0 To Me.grvPerson.ColumnCount - 1
            Me.grvPerson.Columns(i).HeaderText = x_person.getPoInfo.getPOInfoColumn(i).getColumnName.ToUpper
        Next
    End Function

    Private Sub newCouncilMni_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles newCouncilMni.Click
        Dim frmCouncil As FrmCouncil = New FrmCouncil(New X_Council(-1), Me)
        frmCouncil.Visible = True
    End Sub

    Private Sub grvCouncil_CellMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles grvCouncil.CellMouseClick
        If (e.RowIndex < 0 Or e.RowIndex > Me.councilDA.Rows.Count - 1) Then
            Return
        End If
        Me._curRowCouncilSelected = e.RowIndex
        Me.grvCouncil.SelectionMode = DataGridViewSelectionMode.RowHeaderSelect
        Me.grvCouncil.Rows(e.RowIndex).Selected = True

        specifyCorrespondingDetail()
    End Sub

    Private Sub grvCouncil_RowHeaderMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles grvCouncil.RowHeaderMouseClick
        If (e.RowIndex < 0 Or e.RowIndex > Me.councilDA.Rows.Count - 1) Then
            Return
        End If
        Me._curRowCouncilSelected = e.RowIndex
        Me.grvCouncil.SelectionMode = DataGridViewSelectionMode.RowHeaderSelect
        Me.grvCouncil.Rows(e.RowIndex).Selected = True
        specifyCorrespondingDetail()
    End Sub


    

    Public Function loadDisposal() As Boolean Implements SuperForm.loadDisposal

    End Function

    Public Function loadFA() As Boolean Implements SuperForm.loadFA

    End Function

    Public Function loadHandover() As Boolean Implements SuperForm.loadHandover

    End Function

    Public Function loadInventory() As Boolean Implements SuperForm.loadInventory

    End Function

    Public Function loadRepair() As Boolean Implements SuperForm.loadRepair

    End Function

    Public Function specifyCorrespondingDetail1() As Boolean Implements SuperForm.specifyCorrespondingDetail

    End Function

    Public Function specifyCorrespondingDisposal() As Boolean Implements SuperForm.specifyCorrespondingDisposal

    End Function

    Public Function specifyCorrespondingHandover() As Boolean Implements SuperForm.specifyCorrespondingHandover

    End Function

    Public Function specifyCorrespondingInventory() As Boolean Implements SuperForm.specifyCorrespondingInventory

    End Function

    Public Function specifyCorrespondingRepair() As Boolean Implements SuperForm.specifyCorrespondingRepair

    End Function

    Private Sub formCouncilMni_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles formCouncilMni.Click
        If (Me.councilDA Is Nothing) Then
            Return
        End If

        If (Me._curRowCouncilSelected < 0 Or (Me.councilDA.Rows.Count - 1) < Me._curRowCouncilSelected) Then
            Return
        End If

        Dim x_council As New X_Council(-1)
        x_council.setValue(I_Council.ColumnName_id, Me.councilDA.Rows(Me._curRowCouncilSelected)(I_Council.ColumnName_id))
        x_council.setValue(I_Council.ColumnName_DiaDiem, Me.councilDA.Rows(Me._curRowCouncilSelected)(I_Council.ColumnName_DiaDiem))
        x_council.setValue(I_Council.ColumnName_DienGiai, Me.councilDA.Rows(Me._curRowCouncilSelected)(I_Council.ColumnName_DienGiai))
        x_council.setValue(I_Council.ColumnName_ThoiGian, Me.councilDA.Rows(Me._curRowCouncilSelected)(I_Council.ColumnName_ThoiGian))
        x_council.setValue(I_Council.ColumnName_Type, Me.councilDA.Rows(Me._curRowCouncilSelected)(I_Council.ColumnName_Type))

        x_council.setBehave(CONSTANT.BEHAVE_TYPE._UPDATE)

        Dim frmCouncil As FrmCouncil = New FrmCouncil(x_council, Me)
        frmCouncil.Visible = True

    End Sub

    Private Sub grvPerson_CellMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles grvPerson.CellMouseClick
        If (e.RowIndex < 0 Or e.RowIndex > Me.detailDA.Rows.Count - 1) Then
            Return
        End If

        If (Me.detailDA Is Nothing) Then
            Return
        End If

        Dim i As Integer = 0
        For i = 0 To Me.detailDA.Rows.Count - 1
            Dim da As DataTable = grvPerson.DataSource
            If (Me.detailDA.Rows(i)(I_Handover.ColumnName_id).Equals(da.Rows(e.RowIndex)(I_Handover.ColumnName_id))) Then
                Me._curRowDetailSelected = i
                Exit For
            End If
        Next

        Me.grvPerson.SelectionMode = DataGridViewSelectionMode.RowHeaderSelect
        Me.grvPerson.Rows(e.RowIndex).Selected = True
    End Sub

    Private Sub grvPerson_RowHeaderMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles grvPerson.RowHeaderMouseClick
        If (Me.detailDA Is Nothing) Then
            Return
        End If

        Dim i As Integer = 0
        For i = 0 To Me.detailDA.Rows.Count - 1
            Dim da As DataTable = grvPerson.DataSource
            If (Me.detailDA.Rows(i)(I_Handover.ColumnName_id).Equals(da.Rows(e.RowIndex)(I_Handover.ColumnName_id))) Then
                Me._curRowDetailSelected = i
                Exit For
            End If
        Next

        Me.grvPerson.SelectionMode = DataGridViewSelectionMode.RowHeaderSelect
        Me.grvPerson.Rows(e.RowIndex).Selected = True
    End Sub

    Private Sub delCouncilMni_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles delCouncilMni.Click
        If (Me.councilDA Is Nothing) Then
            Return
        End If

        If (Me._curRowCouncilSelected < 0 Or (Me.councilDA.Rows.Count - 1) < Me._curRowCouncilSelected) Then
            Return
        End If

        Dim x_council As New X_Council(-1)
        x_council.setValue(I_Council.ColumnName_id, Me.councilDA.Rows(Me._curRowCouncilSelected)(I_Council.ColumnName_id))
        x_council.delete()

        Me.loadCouncil()
        Me.specifyCorrespondingDetail()
    End Sub

    Private Sub searchCouncilMni_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles searchCouncilMni.Click
        Dim searchBox As SearchBox = New SearchBox(Me.grvCouncil, I_Council.TableName)
        searchBox.Visible = True
    End Sub

    Private Sub newMemberMni_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles newMemberMni.Click
        If (Me.councilDA Is Nothing) Then
            Return
        End If

        If (Me._curRowCouncilSelected < 0 Or (Me.councilDA.Rows.Count - 1) < Me._curRowCouncilSelected) Then
            Return
        End If

        Dim x_council As New X_Council(-1)
        x_council.setValue(I_Council.ColumnName_id, Me.councilDA.Rows(Me._curRowCouncilSelected)(I_Council.ColumnName_id))
        Dim frmPerson As FrmPerson = New FrmPerson(x_council, New X_Person(-1), Me)
        frmPerson.Visible = True
    End Sub

    Private Sub formMemberMni_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles formMemberMni.Click
        If (Me.councilDA Is Nothing) Then
            Return
        End If

        If (Me._curRowCouncilSelected < 0 Or (Me.councilDA.Rows.Count - 1) < Me._curRowCouncilSelected) Then
            Return
        End If

        If (Me._curRowDetailSelected < 0 Or (Me.detailDA.Rows.Count - 1) < Me._curRowDetailSelected) Then
            Return
        End If

        Dim x_person As New X_Person(-1)
        x_person.setValue(I_Person.ColumnName_ChucVu, Me.detailDA.Rows(Me._curRowDetailSelected)(I_Person.ColumnName_ChucVu))
        x_person.setValue(I_Person.ColumnName_CouncilID, Me.detailDA.Rows(Me._curRowDetailSelected)(I_Person.ColumnName_CouncilID))
        x_person.setValue(I_Person.ColumnName_DaiDien, Me.detailDA.Rows(Me._curRowDetailSelected)(I_Person.ColumnName_DaiDien))
        x_person.setValue(I_Person.ColumnName_DienGiai, Me.detailDA.Rows(Me._curRowDetailSelected)(I_Person.ColumnName_DienGiai))
        x_person.setValue(I_Person.ColumnName_HoTen, Me.detailDA.Rows(Me._curRowDetailSelected)(I_Person.ColumnName_HoTen))
        x_person.setValue(I_Person.ColumnName_id, Me.detailDA.Rows(Me._curRowDetailSelected)(I_Person.ColumnName_id))

        x_person.setBehave(CONSTANT.BEHAVE_TYPE._UPDATE)

        Dim frmPerson As New FrmPerson(Nothing, x_person, Me)
        frmPerson.Visible = True

    End Sub

    Private Sub delMemberMni_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles delMemberMni.Click
        If (Me.detailDA Is Nothing) Then
            Return
        End If

        If (Me._curRowDetailSelected < 0 Or (Me.detailDA.Rows.Count - 1) < Me._curRowDetailSelected) Then
            Return
        End If

        Dim x_person As New X_Person(-1)
        x_person.setValue(I_Person.ColumnName_id, Me.detailDA.Rows(Me._curRowDetailSelected)(I_Person.ColumnName_id))
        x_person.delete()

        Me.loadDetail()
    End Sub

    Private Sub searchMemberMni_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles searchMemberMni.Click
        Dim searchBox As SearchBox = New SearchBox(Me.grvPerson, I_Person.TableName)
        searchBox.Visible = True
    End Sub
End Class